﻿namespace NerdWare.ScriptInjector
{
    using System;
    using System.IO;
    using System.Xml.Serialization;

    using NerdWare.Crm2011VsTools.CrmScriptInjector;
    using NerdWare.Crm2011VsTools.CrmScriptInjector.Configuration;
    using NerdWare.Crm2011VsTools.CrmScriptInjector.EventArgs;

    class Program
    {
        static void Main(string[] args)
        {
            Config config = null;

            PrintHeader();

            if (args.Length == 1)
            {
                config = GetConfigByXml(args[0]);
            }

            if (config == null)
            {
                config = GetConfigByConsole();
            }
            
            Console.WriteLine("[ENTER] to start the Proxy.");
            Console.ReadLine();

            var scriptProxy = new ScriptProxy();

            scriptProxy.UrlChanged += UrlChanged;
            scriptProxy.MessageRaised += MessageRaised;

            scriptProxy.StartProxy(config);

            Console.WriteLine("Press [ENTER] to stop the Proxy");
            Console.ReadLine();
            scriptProxy.StopProxy();

            scriptProxy.UrlChanged -= UrlChanged;
            scriptProxy.MessageRaised -= MessageRaised;
            scriptProxy.Dispose();

            Console.WriteLine("Proxy stopped. [ENTER] to close the Console.");

            Console.ReadLine();
        }

        private static void UrlChanged(object sender, UrlChangedEventArgs e)
        {
            Console.WriteLine("Found File {0}", e.FileName);
        }

        private static void MessageRaised(object sender, MessageRaisedEventArgs e)
        {
            if (e.MessageType >= MessageType.Info)
            Console.WriteLine("[{0}] {1}", e.MessageType, e.Message);
        }

        private static void PrintHeader()
        {
            Console.WriteLine("*************************************************************************");
            Console.WriteLine("* ScriptInjector Console Application                                    *");
            Console.WriteLine("*-----------------------------------------------------------------------*");
            Console.WriteLine("* visit : http://crm2011vstools.codeplex.com                            *");
            Console.WriteLine("* mail  : Lars.Mueller@crmpartners.de                                   *");
            Console.WriteLine("*************************************************************************");
        }

        private static Config GetConfigByConsole()
        {
            Config config = new Config();

            Console.WriteLine("Getting Configuration by Console.");
            Console.WriteLine("---------------------------------");
            Console.WriteLine("1. Please enter Url from CRMServer that should be monitored (like 'http://servername.domain.com' or 'https://organizationName.domain.com'.");
            Console.Write("   value: ");
            config.CrmServerUrl = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine("2. Please enter OrganizationName (like 'MyOrganization' or '' if IFD.");
            Console.Write("   value: ");
            config.OrganizationName = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine("3. Please enter Path to your Scripts (like 'c:\folder\folder\folder\'.");
            Console.Write("   value: ");
            config.ScriptPath = Console.ReadLine();

            return config;
        }

        private static Config GetConfigByXml(string path)
        {
            Config config;
            XmlSerializer ds = new XmlSerializer(typeof(Config));
            using (Stream s = File.OpenRead(path))
            {
                config = (Config)ds.Deserialize(s);
            }

            return config;
        }
    }
}
